/**
 *	模块字段
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */

package com.framework.Module.flow.flowelement;



import com.framerock.db.DB;
import com.framerock.baselib.Json;
import com.framerock.baselib.Patter;
import com.framerock.baselib.Rock;
import com.framework.ACore.Model;
import com.framework.Flow.FlowListener;
import com.framework.Model.ModelMode;
import com.framework.Table.Table;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class FlowListenerflowelement extends FlowListener {



    //有模块js
    public Boolean onModejs() { return true;}

    public ArrayList<Map<String, String>> onStore(String action, String params) {
        if(action.equals("fieldsdata"))return fieldsdata();
        return null;
    }



    public Map<String, String> onDatabefore(){
        String modeid = mRequest.get("modeid", "0");
        String where  = "`modeid`="+modeid+"";
        return Rock.getMap(new String[]{
                "where", where
        });
    }

    public Map<String, String> onDataafter()
    {
        Map<String, String> da = Rock.getMap();
        if(mRequest.LOADCI==1){
            da.put("modearr", Json.getListString(ModelMode.get(this.nDb).getMdearr()));
        }

        return da;
    }

    //读取字段数据源
    public ArrayList<Map<String, String>> fieldsdata()
    {
        String mkid = mRequest.get("mkid");
        if(Rock.isEmpt(mkid) || !Patter.isnumber(mkid))return null;

        Map<String, String> da,mrs = Model.get(this.nDb, "flowset").getOne(mkid);
        if(mrs==null)return null;
        String table = mrs.get("table");

        ArrayList<Map<String, String>> rows = this.nDb.getColumns(DB.chuliPerfix(table));
        storeList = null;
        String fields,name;
        if(rows!=null)for(int i=0; i<rows.size() ;i++){
            da = rows.get(i);
            fields = da.get("fields");
            if(fields.equals("id"))continue;
            name = da.get("sm").split("@")[0];
            adddList(fields, name,fields+","+da.get("type"));
        }
        return storeList;
    }


    public Map<String, String> onSavebefore(Map<String, String> da, String mid, Boolean isadd)
    {
        String modeid = da.get("modeid");
        String fields = da.get("fields");
        int to = mFlow.mModel.getCount("id<>"+mid+" and `modeid`="+modeid+" and `fields`='"+fields+"'");
        if(to>0)return mFlow.setErrors("字段("+fields+")已经存在");

        Map<String, String> map = Rock.getMap();
        String dbtype   = da.get("dbtype");
        String type     = da.get("fieldstype");
        int lens        = Integer.parseInt(Rock.getMapString(da, "lens", "0"));
        if(lens==0)lens = 100;
        if((type.equals("textarea") || type.equals("changedeptusercheck")) && lens<=100)lens = 2000;
        if(Rock.isEmpt(dbtype)){
            dbtype = "varchar("+lens+")";
            if(type.indexOf("date")>-1)dbtype = type;
            if(type.equals("number"))dbtype = "int(11)";
            if(type.equals("checkbox"))dbtype = "tinyint(1)";
            map.put("savefields", "dbtype="+dbtype+"");
        }
        return map;
    }

    private String nowtable;
    public void onSaveafter(Map<String, String> da, String mid, Boolean isadd) {
        //int islu = Rock.getMapint(da, "islu");
        //if(islu==0)return;

        String dbtype   = da.get("dbtype");
        String fields   = da.get("fields");
        String type     = da.get("fieldstype");
        if(Rock.isEmpt(dbtype) || fields.equals("id") || fields.indexOf("temp_")==0 || fields.indexOf("base_")==0)return;

        String name     = da.get("name");
        String dev      = da.get("dev");
        String data     = da.get("data");

        Map<String, String> mrs = Model.get(this.nDb, "flowset").getOne(da.get("modeid"));
        nowtable        = DB.chuliPerfix(mrs.get("table"));

        ArrayList<Map<String, String>> farr = this.nDb.getColumns(nowtable);
        Map<String, String> da1,da2;
        Map<String, Map<String, String>> fobj = new HashMap<String, Map<String, String>>();
        Table tab = new Table(this.nDb);
        for(int i=0;i<farr.size();i++){
            da1      = farr.get(i);
            fobj.put(da1.get("fields"), da1);
        }

        if(dbtype.indexOf("date")>-1)dev = null;
        da2 = Rock.getMap(new String[]{
                "fields", fields,
                "type", dbtype,
                "dev", dev,
                "sm", name
        });
        this.createfields(fobj.get(fields), tab.getFieldstr(da2), dbtype);

        if(type.indexOf("change")==0 && !Rock.isEmpt(data)){
            String arr1[] = data.split(",");
            String ndbtype = dbtype;
            fields  = arr1[0];
            dev     = "";
            if(type.indexOf("check")==-1){
                dbtype = "int(11)";
                dev = "0";
                ndbtype = "";
            }
            da2 = Rock.getMap(new String[]{
                    "fields", fields,
                    "type", dbtype,
                    "dev", dev,
                    "sm", name+"ID"
            });
            this.createfields(fobj.get(fields), tab.getFieldstr(da2), ndbtype);
        }

        if(type.indexOf("selectdata")==0 && !Rock.isEmpt(data)){
            String arr1[] = data.split(",");
            if(arr1.length>1) {
                fields = arr1[1];
                dev = "";
                da2 = Rock.getMap(new String[]{
                        "fields", fields,
                        "type", dbtype,
                        "dev", dev,
                        "sm", name + "ID"
                });
                this.createfields(fobj.get(fields), tab.getFieldstr(da2), "");
            }
        }
    }

    //创建字段或者不一样时更新
    private void createfields(Map<String, String> rs, String nstr, String dbtype)
    {
        if(rs == null){
            String sql = "ALTER TABLE `"+nowtable+"` ADD "+nstr+"";
            this.nDb.Query(sql);
        }else{
            String ntype = rs.get("type");
            if(!Rock.isEmpt(ntype) && !Rock.isEmpt(dbtype) && !ntype.equals(dbtype)){
                String sql = "ALTER TABLE `"+nowtable+"` MODIFY "+nstr+"";
                this.nDb.Query(sql);
            }
        }
    }
}
